// 开发环境
// const host = 'http://192.168.60.122:8099';
// 测试环境
// const host = 'http://121.37.136.48:8099'
// 生产环境
const host = 'https://www.wxidev.top'


/** 
 * HTTP 请求，
 * URL 接口 
 * postData：参数 
 * doSuccess：成功的回调函数
 * doFail：失败的回调函数 
 */             
 
function Urltoken() {
  let currentUser = (wx.getStorageSync('userInfo')).token;
  return currentUser ? currentUser : undefined
}

function doRequest(url, method, theData,responseType) {
  let theUrl = url
  if (url.indexOf('http') === -1) {
    theUrl = host + url
  } 
  let promise = new Promise((resolve, reject)=>{
    wx.request({
      url: theUrl,
      header: {
        "content-type": "application/json;charset=UTF-8",
        "Authorization": "token " + Urltoken(),
      },
      data: theData,
      method: method,
      responseType:responseType,
      success: function (res) {
        if (res.statusCode === 401) {
          wx.showToast({ 
            title: '您的账号登录已失效, 请重新登录',
            icon: 'none'
          })
          setTimeout(() => {
            wx.removeStorageSync('tenantInfo')
            wx.reLaunch({
              url: '/packageLogin/pages/login/login'
            })
          }, 1500)
          return false
        }
        resolve(res);
      },
      fail: function (XHR) {
        reject(XHR)
      },
    })
  })
  return promise;
}

module.exports = {
  doRequest
}
module.exports.http = host;
module.exports.doRequest = doRequest;